﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using DBService;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit.MEP.Opening.Properties;

namespace YArchitech.Revit.MEP.Opening
{
	public partial class OpenHoleConfigForm : System.Windows.Forms.Form
	{
		public OpenHoleConfigForm()
		{
			this.InitializeComponent();
		}

		private void OfferDataConfigForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.m_wndToolTip = new ToolTip();
			this.m_wndToolTip.AutoPopDelay = 1000;
			this.m_wndToolTip.InitialDelay = 1000;
			this.m_wndToolTip.ReshowDelay = 500;
			this.m_wndToolTip.ShowAlways = true;
			this.m_wndToolTip.SetToolTip(this.textOpeningSize, "输入0表示不限制开洞尺寸!");
			string strXmlFilePath = ConfigDataOperate.InitOpenHoleDataConfigFile();
			this.m_cfgData = new ConfigDataOperate.OpenHoleConfigData(strXmlFilePath);
			this.cmbRectOpeningLevelBase.Items.AddRange(ConfigDataOperate.arOpeningLevelBase);
			this.cmbCircleOpeningLevelBase.Items.AddRange(ConfigDataOperate.arOpeningLevelBase);
			this.UpdateCtrlDataShow();
		}

		private void SelbtnServerSel_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.m_cfgData.LocalOperDataFilePath = this.txtLocalPath.Text;
			this.m_cfgData.OperMode = (this.radioButtonLocal.Checked ? ConfigDataOperate.EOperMode.eOperLocal : ConfigDataOperate.EOperMode.eOperServer);
			this.m_cfgData.DimHoleStyle = (this.radioButtonTable.Checked ? ConfigDataOperate.EDimHoleStyle.eDimStyleByTable : ConfigDataOperate.EDimHoleStyle.eDimStyleByLabel);
			this.m_cfgData.DimPrefixItem = this.txtPrefixItem.Text;
			this.m_cfgData.RectOpeningLevelBase = (ConfigDataOperate.EOpeningLevelBase)this.cmbRectOpeningLevelBase.SelectedIndex;
			this.m_cfgData.CircleOpeningLevelBase = (ConfigDataOperate.EOpeningLevelBase)this.cmbCircleOpeningLevelBase.SelectedIndex;
			this.m_cfgData.DimOpening = this.checkBoxDimOpening.Checked;
			this.m_cfgData.DimMajor = this.checkBoxMajor.Checked;
			this.cmbRectOpeningLevelBase.SelectedIndex = (int)this.m_cfgData.RectOpeningLevelBase;
			this.cmbCircleOpeningLevelBase.SelectedIndex = (int)this.m_cfgData.CircleOpeningLevelBase;
			this.m_cfgData.ArchOpeningDimMinSize = Convert.ToDouble(this.textOpeningSize.Text);
			this.m_cfgData.WriteCfgData2Xml();
		}

		private void btnPickPath_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Title = "请选择提资数据xml文件";
			openFileDialog.InitialDirectory = Product.UserDataLocation + "\\";
			openFileDialog.Filter = "协同数据文件(*.xml)|*.xml";
			openFileDialog.FileName = "SynergyData.xml";
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.txtLocalPath.Text = openFileDialog.FileName;
			}
		}

		private void UpdateCtrlDataShow()
		{
			this.radioButtonLocal.Checked = (this.m_cfgData.OperMode == ConfigDataOperate.EOperMode.eOperLocal);
			this.radioButtonServer.Checked = (this.m_cfgData.OperMode == ConfigDataOperate.EOperMode.eOperServer);
			this.txtLocalPath.Text = this.m_cfgData.LocalOperDataFilePath;
			this.radioButtonTable.Checked = (this.m_cfgData.DimHoleStyle == ConfigDataOperate.EDimHoleStyle.eDimStyleByTable);
			this.radioButtonLabel.Checked = (this.m_cfgData.DimHoleStyle == ConfigDataOperate.EDimHoleStyle.eDimStyleByLabel);
			this.txtPrefixItem.Text = this.m_cfgData.DimPrefixItem;
			this.cmbRectOpeningLevelBase.SelectedIndex = (int)this.m_cfgData.RectOpeningLevelBase;
			this.cmbCircleOpeningLevelBase.SelectedIndex = (int)this.m_cfgData.CircleOpeningLevelBase;
			this.checkBoxDimOpening.Checked = this.m_cfgData.DimOpening;
			this.checkBoxMajor.Checked = this.m_cfgData.DimMajor;
			this.textOpeningSize.Text = this.m_cfgData.ArchOpeningDimMinSize.ToString("F0");
		}

		private void UpdateCtrlStatus()
		{
			this.btnPickPath.Enabled = this.radioButtonLocal.Checked;
			this.txtLocalPath.Enabled = this.radioButtonLocal.Checked;
			this.btnServerSel.Enabled = this.radioButtonServer.Checked;
			this.label2.Enabled = this.radioButtonTable.Checked;
			this.txtPrefixItem.Enabled = this.radioButtonTable.Checked;
			this.picTable.Enabled = this.radioButtonTable.Checked;
			this.picLabel.Enabled = this.radioButtonLabel.Checked;
		}

		private void radioButtonLocal_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private void radioButtonServer_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private void radioButtonTable_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private void radioButtonLabel_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private ConfigDataOperate.OpenHoleConfigData m_cfgData;

		private ToolTip m_wndToolTip;
	}
}
